package com.neusoft.neuiotms.dcom.service.impl;

import java.util.List;

import javax.persistence.EntityNotFoundException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.converter.HandoverConverter;
import com.neusoft.neuiotms.dcom.dto.HandoverDTO;
import com.neusoft.neuiotms.dcom.dto.UserDutyInfoDTO;
import com.neusoft.neuiotms.dcom.model.Handover;
import com.neusoft.neuiotms.dcom.model.enums.HandoverStatus;
import com.neusoft.neuiotms.dcom.repository.HandoverRepository;
import com.neusoft.neuiotms.dcom.service.HandoverService;
import com.neusoft.neuiotms.dcom.service.TeamGroupService;

@Service
public class HandoverServiceImpl implements HandoverService {

    @Autowired
    private HandoverRepository handoverRepository;
    @Autowired
    private HandoverConverter handoverConverter;
    @Autowired
    private TeamGroupService teamGroupService;

    @Override
    public List<HandoverDTO> index(final Searchable searchable) {
        final List<Handover> models = this.handoverRepository.index(searchable);
        return this.handoverConverter.toListDTO(models);
    }

    @Override
    public Page<HandoverDTO> search(final Pageable pageable, final Searchable searchable) {
        final Page<Handover> result = this.handoverRepository.search(pageable, searchable);
        return this.handoverConverter.toPageDTO(result);
    }

    @Override
    public HandoverDTO create(final HandoverDTO dto, final UserBean user) {

        final List<UserDutyInfoDTO> teamList = this.teamGroupService.dutyInfo(user);
        if (CollectionUtils.isEmpty(teamList)) {
            throw new UnitedException("当前登录人没有分配值班小组,不能创建值班记录！");
        }

        // 设置登录人职能
        dto.setFunctionalGroupId(teamList.get(0).getFunctionalGroupId());

        final Handover model = this.handoverConverter.toModel(dto);
        this.handoverRepository.save(model);
        return this.handoverConverter.toDTO(model);
    }

    @Override
    public HandoverDTO update(final Long id, final HandoverDTO dto) {

        Handover model;
        try {
            model = this.handoverRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        dto.setId(model.getId());
        model = this.handoverConverter.toModel(dto);
        this.handoverRepository.save(model);

        return this.handoverConverter.toDTO(model);
    }

    @Override
    public HandoverDTO details(final Long id) {

        Handover model;
        try {
            model = this.handoverRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        return this.handoverConverter.toDTO(model);
    }

    @Override
    public void delete(final Long id) {
        this.handoverRepository.deleteById(id);
    }

    @Override
    public HandoverDTO updateStatus(final Long id, final HandoverStatus status) {

        Handover model;
        try {
            model = this.handoverRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        model.setStatus(status);
        this.handoverRepository.save(model);

        return this.handoverConverter.toDTO(model);
    }

}
